package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsUserAccountService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DayCountStatis;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */

@Service
public class DtsUserServiceImpl implements DtsUserService {
    @Autowired
    DtsUserMapper userMapper;

    @Autowired
    StatMapper statMapper;
    @Autowired
    private DtsUserAccountService userAccountService;
    @Override
    public Integer findTotal() {
        LambdaQueryWrapper<DtsUser> wrapper
                = new LambdaQueryWrapper<>();

        wrapper.eq(DtsUser::getDeleted ,false);

        Integer count = userMapper.selectCount(wrapper);

        return count;
    }

    @Override
    public List<DayStatis> queryDayStatis(int rang) {

        return statMapper.queryUserStatis(rang);
    }

    @Override
    public IPage<DtsUser> queryPage(Integer page, Integer limit, String sort, String order, String username, String mobile) {

        IPage<DtsUser> iPage = new Page<>(page, limit);

        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(username)){
            wrapper.like("username", username);
        }

        if(!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile", mobile);
        }

        if(order.equalsIgnoreCase("desc")){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        iPage = userMapper.selectPage(iPage, wrapper);

        return iPage;
    }

    @Override
    public DtsUser findUserByUserId(Integer userId) {

        DtsUser user = userMapper.selectById(userId);

        return user;
    }

    @Override
    public IPage<DtsUser> findAll(String username, String mobile, Integer limit, Integer page, String sort, String order) {
        IPage<DtsUser> ipage = new Page(page,limit);
        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        if(!StringUtils.isEmpty(mobile)){
            wrapper.like("username",mobile);
        }
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsUser> p = userMapper.selectPage(ipage, wrapper);
        return p;
    }

    @Override
    public List<DayCountStatis> findUserCount(int day) {
        return statMapper.findUserCount(day);
    }
    @Transactional
    @Override
    public Object saveStatusAndUserLevel(DtsUserAccount userAccount) {
        if(userAccount.getUserId() == null){
            return ResponseUtil.badArgument();
        }
        DtsUser user = new DtsUser();
        user.setId(userAccount.getUserId());
        user.setUpdateTime(new Date());
        user.setStatus(0);
        user.setUserLevel(2);
        userMapper.updateById(user);
        userAccount.setModifyTime(new Date());
        userAccountService.saveSett(userAccount);
        return ResponseUtil.ok();
    }
}
